import axios from 'axios'
import { Dialog, Toast } from 'vant'

axios.interceptors.request.use(config => {
  Toast.loading({
    duration: 0,
    message: '加载中...',
    forbidClick: true
  })
  return config
}, error => {
  return Promise.reject(error)
})

axios.interceptors.response.use(
  response => {
    Toast.clear()
    return response
  },
  error => {
    Toast.clear()
    Dialog.alert({
      title: '提示',
      message: '网络请求失败'

    })
    console.log(error)
  }
)

const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MTc4NzUzNzIsInVzZXJuYW1lIjoiOGFmYWM5YWE1OGZjZTU3NjAxNTkwNjE4ZThjNTJiYzQifQ.gs9uujsCToaFu1uMEY1uLSdrjOPgVbKdc4jeYZflVsg'
axios.defaults.headers.common.token = token

const instance = axios.create({
  baseURL: 'https://qccxweb.nbwhlt.com/',
  timeout: 10000
})

export const get = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    instance.get(url, { params }).then((response) => {
      resolve(response.data)
    }, err => {
      reject(err)
    })
  })
}

export const post = (url, data = {}) => {
  return new Promise((resolve, reject) => {
    instance.post(url, data, {
      headers: {
        'Content-Type': 'application/json'
      }
    }).then((response) => {
      resolve(response.data)
    }, err => {
      reject(err)
    })
  })
}
